package com.example.wyemail.MySwing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 激活弹窗
 *
 * @author fxab
 * @date 2024/06/14
 */
public class MyJDialog extends JDialog {                           //创建新类继承JDialog类

    /**
     * 激活码
     */
    String activationCode = "";

    /**
     * 激活
     */
    JTextField activation = new JTextField();

    /**
     * 我jdialog
     *
     * @param frame 框架
     */
    public MyJDialog(Frame frame) {
        //实例化一个JDialog类对象，指定对话框的父窗体、窗体标题和类型
        super(frame, "激活自动工具", true);

        Container container = getContentPane();                    //创建一个容器
        container.setLayout(null);
        JLabel activationJL = new JLabel("激活码：");
        activationJL.setBounds(10,10,100,20);
        container.add(activationJL);
        activation.setBounds(55,10,200,20);
        container.add(activation);
        JButton okBtn = new JButton("激活");
        okBtn.setBounds(90,60,100,20);
        container.add(okBtn);
        setBounds(700, 400, 300, 150);          //设置对话框窗体大小
        okBtn.addActionListener(new ActivateAction());

    }


    private class ActivateAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            activationCode = activation.getText();
            if (!activationCode.isEmpty()) {
                try {
                    //todo 向服务器发送激活请求
                    System.out.println(activationCode);
                    dispose();
                    //打开主页面
                    SwingUtilities.invokeLater(() -> {
                        MainFrame frame = new MainFrame();
                        frame.setVisible(true);
                    });

                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(MyJDialog.this, "无法执行程序: " + ex.getMessage());
                }
            } else {
                JOptionPane.showMessageDialog(MyJDialog.this,"请输入激活码");
            }
        }
    }


}
